package framework.eea.event;

import java.util.LinkedList;
import java.util.List;
import framework.eea.StateBasedGame;
import framework.eea.gui.JFrameGameContainer;
import framework.eea.interfaces.Action;

/**
 *
 * @author Markus Schr&ouml;der
 */
public abstract class Event extends Component {

    private List<Action> actions;

    public Event(String id) {
        super(id);
        actions = new LinkedList<Action>();
    }

    public final void addAction(Action action) {
        actions.add(action);
    }

    /**
     * removes all {@link eea.engine.action.Action} instances from the event
     */
    public void clearActions() {
        actions.clear();
    }

    protected abstract boolean performAction(JFrameGameContainer gc, StateBasedGame sb, int delta);

    public void removeAction(Action action) {
        actions.remove(action);
    }

    public void removeAction(int index) {
        actions.remove(index);
    }

    @Override
    public void update(JFrameGameContainer gc, StateBasedGame sb, int delta) {
        // really perform the action(s)?
        if (performAction(gc, sb, delta)) {
            // yes => loop over all actions...
            for (Action action : actions) {
                // ... and update them
                action.update(gc, sb, delta, this);
            }
        }
    }
}
